﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Net;
using System.IO;


using SEDCupConsole;


namespace SedCup.Code.Control
{
    public class ConnectExample
    {   
        /// <summary>
        /// Meg kell hívni a konstuktort utána feltváltva lépnek.
        /// </summary>
        public ConnectExample()
        {
            //System.Console.WriteLine("================");

            
            StartBotClientThread();
            //System.Console.WriteLine("================");
            //for (int i = 0; i < 10; i++ )
            //    SEDCupConsole.Example.ConnectToServerClient1("{\"turn\":0,\"hire\":[0,0,0],\"move\":[],\"dojob\":[],\"fire\":[]}");
        }
        public static void ConnectToServerClient1(Object param)
        {

            string url = @"http://localhost:3723/sedcup2013/teams/Client1";
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
            setAuthInfo(request);
            request.Method = "POST";
            request.ContentType = "application/json";
            string jsonData = "{\"turn\":0,\"hire\":[0,0,0],\"move\":[],\"dojob\":[],\"fire\":[]}";
            while (true)
            {
                request = (HttpWebRequest)HttpWebRequest.Create(url);
                setAuthInfo(request);
                request.Method = "POST";
                request.ContentType = "application/json";
                if (!string.IsNullOrEmpty(jsonData))
                {
                    Stream requestStream = request.GetRequestStream();
                    byte[] restData = Encoding.UTF8.GetBytes(jsonData);
                    requestStream.Write(restData, 0, restData.Length);
                    requestStream.Close();
                }

                try
                {
                    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                    using (StreamReader reader = new StreamReader(response.GetResponseStream()))
                    {
                        string res = reader.ReadToEnd();
                        Console.WriteLine("Received content: " + res);
                    }

                }
                catch (Exception ex)
                {
                    Console.WriteLine("Excepion occured while connecting to the server" + ex.Message + "\n" + ex.StackTrace);
                }

            }
        }

        /// <summary>
        /// Indít egy szálat ami mindig "üres" választ ír
        /// </summary>
        public static void StartBotClientThread()
        {
            Thread thread = new Thread(new ParameterizedThreadStart(ConnectToServerClient2));
            thread.Start();
            //Thread thread2 = new Thread(new ParameterizedThreadStart(ConnectToServerClient1));
            //thread2.Start();
        }


        public static void ConnectToServerClient2(Object param)
        {

                string url = @"http://localhost:3723/sedcup2013/teams/Client2";
                HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
                setAuthInfo(request);
                request.Method = "POST";
                request.ContentType = "application/json";
                string jsonData = "{\"turn\":0,\"hire\":[0,0,0],\"move\":[],\"dojob\":[],\"fire\":[]}";
                while (true)
                {
                    request = (HttpWebRequest)HttpWebRequest.Create(url);
                    setAuthInfo(request);
                    request.Method = "POST";
                    request.ContentType = "application/json";
                if (!string.IsNullOrEmpty(jsonData))
                {
                    Stream requestStream = request.GetRequestStream();
                    byte[] restData = Encoding.UTF8.GetBytes(jsonData);
                    requestStream.Write(restData, 0, restData.Length);
                    requestStream.Close();
                }

                try
                {
                    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                    using (StreamReader reader = new StreamReader(response.GetResponseStream()))
                    {
                        string res = reader.ReadToEnd();
                        Console.WriteLine("Received content: " + res);
                    }

                }
                catch (Exception ex)
                {
                    Console.WriteLine("Excepion occured while connecting to the server" + ex.Message + "\n" + ex.StackTrace);
                }

            }
        }

        public static void setAuthInfo(HttpWebRequest request)
        {
            string authInfo = "login" + ":" + "secret";
            authInfo = Convert.ToBase64String(Encoding.UTF8.GetBytes(authInfo));
            request.Headers["Authorization"] = "Basic " + authInfo;
        }

    }
}
